<template>
  <div>
    <TypeNav />

    <!--list-content-->
    <div class="main">
      <div class="container">
        <!--bread-->
        <div class="bread clearfix">
          <div class="fl sui-breadcrumb">
            <a href="#">全部结果</a>
          </div>
          <ul class="fl sui-tag">
            <li class="with-x" v-if="searchParams.categoryName">
              {{ searchParams.categoryName
              }}<i @click="removeCategoryName">×</i>
            </li>
            <!-- 关键字的面包屑 -->
            <li class="with-x" v-if="searchParams.keyword">
              {{ searchParams.keyword }}<i @click="removeKeyword">×</i>
            </li>
            <!-- 品牌的面包屑 -->
            <li class="with-x" v-if="searchParams.trademark">
              {{ searchParams.trademark }}<i @click="removeTradeMark">×</i>
            </li>
            <!--平台的售卖的属性值展示 -->
            <li
              class="with-x"
              v-for="(newprop, index) in searchParams.props"
              :key="index"
            >
              {{ newprop.split(":")[0] }}<i @click="removeAttr(index)">×</i>
            </li>
          </ul>
        </div>
        <SearchSelector
          @trademarkInfo="handletrademark"
          @attrInfo="handleattr"
        />
        <!--details-->
        <div class="details">
          <div class="sui-navbar">
            <div class="filter">
              <ul class="sui-nav">
                <li :class="{ active: isOne }" @click="changeOrder('1')">
                  <a
                    >综合<span
                      v-show="isOne"
                      class="iconfont"
                      :class="{
                        'icon-icon_up': isAsc,
                        'icon-icon_down': isDesc,
                      }"
                    ></span
                  ></a>
                </li>

                <li :class="{ active: isTwo }" @click="changeOrder('2')">
                  <a
                    >价格
                    <span
                      v-show="isTwo"
                      class="iconfont"
                      :class="{
                        'icon-icon_up': isAsc,
                        'icon-icon_down': isDesc,
                      }"
                    >
                    </span>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="goods-list">
            <ul class="yui3-g">
              <li class="yui3-u-1-5" v-for="good in goodsList" :key="good.id">
                <div class="p-img">
                  <router-link :to="`/detail/${good.id}`">
                    <img v-lazy="good.defaultImg"
                  /></router-link>
                </div>
                <div class="price">
                  <strong>
                    <em>¥</em>
                    <i>{{ good.price }}.00</i>
                  </strong>
                </div>
                <div class="attr">
                  <a
                    target="_blank"
                    href="item.html"
                    title="促销信息，下单即赠送三个月CIBN视频会员卡！【小米电视新品4A 58 火爆预约中】"
                    >{{ good.title }}</a
                  >
                </div>
                <div class="commit">
                  <i class="command">已有<span>2000</span>人评价</i>
                </div>
                <div class="operate">
                  <a class="sui-btn btn-bordered btn-danger">加入购物车</a>
                  <a href="javascript:void(0);" class="sui-btn btn-bordered"
                    >收藏</a
                  >
                </div>
              </li>
            </ul>
          </div>
          <el-pagination
            :current-page="searchParams.pageNo"
            :total="total"
            :page-size="searchParams.pageSize"
            :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
            style="text-align: center"
            layout="prev, pager, next, jumper, ->, sizes, total"
            @current-change="getPageNo"
            @size-change="handleSizeChange"
            :continues="5"
          />
        </div>
      </div>

      <!-- 底部 -->
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import SearchSelector from "./SearchSelector";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Search",
  components: {
    SearchSelector,
  },
  data() {
    return {
      searchParams: {
        //产品相应的id
        category1Id: "",
        category2Id: "",
        category3Id: "",
        //产品的名字
        categoryName: "",
        //搜索的关键字
        keyword: "",
        //排序:初始状态应该是综合且降序
        order: "1:desc",
        //第几页
        pageNo: 1,
        //每一页展示条数
        pageSize: 3,
        //平台属性的操作
        props: [],
        //品牌
        trademark: "",
      },
    };
  },
  beforeMount() {
    console.log(this.$route.params);
    // this.searchPrams.categoryName = this.$route.query.categoryName;
    // this.searchPrams.category1Id = this.$route.query.category1Id;
    // this.searchPrams.category2Id = this.$route.query.category2Id;
    // this.searchPrams.category3Id = this.$route.query.category3Id;
    //浅拷贝
    Object.assign(this.searchParams, this.$route.params, this.$route.query);
    // this.searchParams={...this.searchParams,...this.$route.params,...this.$route.query}
  },
  mounted() {
    //随着参数不同
    this.getData();
  },
  computed: {
    isAsc() {
      return this.searchParams.order.includes("asc");
    },
    isDesc() {
      return this.searchParams.order.includes("desc");
    },
    isOne() {
      return this.searchParams.order.includes("1");
    },
    isTwo() {
      return this.searchParams.order.includes("2");
    },

    ...mapGetters("search", ["goodsList", "total"]),
  },
  methods: {
    // 2.服务器存储成功，路由跳转
    // 3.失败给用户提

    changeOrder(type) {
      let originOrder = this.searchParams.order;
      //将字符串分割成数组取出来
      let num = originOrder.split(":")[0];
      let sort = originOrder.split(":")[1];

      let newOrder = "";
      //改变顺序
      if (type == num) {
        newOrder = `${num}:${sort == "desc" ? "asc" : "desc"}`;
      } else {
        //点击不是同一个按钮，改变num
        newOrder = `${type}:${"desc"}`;
      }
      this.searchParams.order = newOrder;
      this.getData();
      console.log(newOrder);
    },
    removeCategoryName() {
      this.searchParams.category1Id = undefined;
      this.searchParams.category2Id = undefined;
      this.searchParams.category3Id = undefined;

      this.searchParams.categoryName = undefined;
      if (this.$route.params) {
        this.$router.push({ name: "search", params: this.$route.params });
      }
    },
    removeKeyword() {
      //给服务器带的参数searchParams的keyword置空,无params
      this.searchParams.keyword = undefined;
      //通知兄弟组件Header清除关键字
      this.$bus.$emit("clear");
      if (this.$route.query) {
        this.$router.push({ name: "search", query: this.$route.query });
      }
    },

    getData() {
      this.$store.dispatch("search/getSearchList", this.searchParams);
    },
    getPageNo(pageNo) {
      //整理带给服务器参数
      this.searchParams.pageNo = pageNo;
      //再次发请求
      this.getData();
    },
    handleSizeChange(pageSize) {
      this.searchParams.pageSize = pageSize;
      this.getData();
    },

    handletrademark(tm) {
      console.log(tm);
      this.searchParams.trademark = `${tm.tmId}:${tm.tmName}`;
      this.getData();
    },
    removeTradeMark() {
      //将品牌信息置空
      this.searchParams.trademark = undefined;
      //再次发请求
      this.getData();
    },
    handleattr(attr, attrValue) {
      //["属性ID:属性值:属性名"]
      console.log(attr, attrValue);
      //参数格式整理好
      let newprop = `${attrValue}:${attr.attrId}:${attr.attrName}`;
      //数组去重
      //if语句里面只有一行代码：可以省略大花括号
      if (!this.searchParams.props.includes(newprop))
        this.searchParams.props.push(newprop);
      //再次发请求
      this.getData();
    },
    removeAttr(index) {
      //将品牌信息置空
      this.searchParams.props.splice(index, 1);
      //再次发请求
      this.getData();
    },
  },
  watch: {
    // eslint-disable-next-line no-unused-vars
    $route(newValue, oldValue) {
      //每一次请求完毕，应该把相应的1、2、3级分类的id置空的，让他接受下一次的相应1、2、3
      //再次发请求之前整理带给服务器参数
      Object.assign(this.searchParams, this.$route.query, this.$route.params);
      //再次发起ajax请求
      this.getData();
      //分类名字与关键字不用清理：因为每一次路由发生变化的时候，都会给他赋予新的数据
      this.searchParams.category1Id = undefined;
      this.searchParams.category2Id = undefined;
      this.searchParams.category3Id = undefined;
    },
  },
};
</script>

<style lang="scss" scoped>
.main {
  margin: 10px 0;
}
.bread {
  .sui-breadcrumb {
    font-weight: 400;
    padding: 3px 15px;
    a {
      color: #666;
      text-decoration: none;

      &:hover {
        color: #4cb9fc;
      }
    }
  }
  .sui-tag {
    margin-bottom: 18px;

    .with-x {
      font-size: 12px;
      margin: 0 5px 5px 0;
      display: inline-block;
      overflow: hidden;
      color: #000;
      background: #f7f7f7;
      padding: 0 7px;
      height: 20px;
      line-height: 20px;
      border: 1px solid #dedede;
      white-space: nowrap;
      transition: color 400ms;
      cursor: pointer;

      i {
        margin-left: 10px;
        cursor: pointer;
        font: 400 14px tahoma;
        display: inline-block;
        height: 100%;
        vertical-align: middle;
      }

      &:hover {
        color: #28a3ef;
      }
    }
  }
}

.sui-navbar {
  .filter {
    min-height: 40px;

    background: #fbfbfb;
    border: 1px solid #e2e2e2;

    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.065);

    .sui-nav {
      margin: 0 10px 0 0;

      li {
        float: left;
        line-height: 18px;

        a {
          display: block;
          font-size: 14px;
          padding: 11px 15px;
          color: #777;
        }

        &.active {
          a {
            background: #e1251b;
            color: #fff;
          }
        }
      }
    }
  }
}
.goods-list {
  margin: 20px 0;

  ul {
    display: flex;
    flex-wrap: wrap;

    li {
      height: 100%;
      width: 20%;
      margin-top: 10px;
      line-height: 28px;

      .p-img {
        width: 215px;
        height: 255px;
      }

      .price {
        font-size: 18px;
        color: #c81623;

        strong {
          font-weight: 700;

          i {
            margin-left: -5px;
          }
        }
      }

      .attr {
        //        文本显示为两行,超过部分隐藏并使用省略号
        // 修改 width 属性查看效果。
        width: 85%;

        margin-bottom: 8px;
        min-height: 38px;

        line-height: 1.8;
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;

        a {
          color: #333;
          text-decoration: none;
        }
      }
      .commit {
        height: 22px;
        font-size: 13px;
        color: #a7a7a7;

        span {
          font-weight: 700;
          color: #646fb0;
        }
      }

      .operate {
        padding: 12px 15px;

        .sui-btn {
          display: inline-block;
          padding: 2px 14px;
          box-sizing: border-box;

          text-align: center;

          margin-right: 15px;
        }
        .btn-bordered {
          border: 1px solid #8c8c8c;
          color: #8c8c8c;

          &:hover {
            border: 1px solid #666;
            color: #fff !important;
            background-color: #666;
            text-decoration: none;
          }
        }

        .btn-danger {
          border: 1px solid #e1251b;
          color: #e1251b;

          &:hover {
            border: 1px solid #e1251b;
            background-color: #e1251b;
            color: white !important;
            text-decoration: none;
          }
        }
      }
    }
  }
}
</style>
